import time
from rest_framework.throttling import BaseThrottle, SimpleRateThrottle

visit_record = {}

# class VistThrottle(BaseThrottle):
#     def allow_request(self, request, view):
#         remote_ip = self.get_ident()
#         ctime = time.time()
#         if remote_ip not in visit_record:
#             visit_record[remote_ip] = [ctime,]
#             return True
#         history = visit_record.get(remote_ip)
#         print(history)
#         while history[-1] < ctime - 60:
#             history.pop()
#         if len(history) < 3:
#             history.insert(0,ctime)
#             return True
#     def wait(self):
#         pass

class VisitThrottle(SimpleRateThrottle):
    scope = "test"

    def get_cache_key(self, request, view):
        return self.get_ident(request)
